package com.dk.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dk.mapper.MenuMapper;
import com.dk.mapper.RoleMenuMapper;
import com.dk.pojo.Menu;
import com.dk.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@org.springframework.stereotype.Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private RoleMenuMapper roleMenuMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public List<Map<String, Object>> getZv() {

        List<Menu> me = menuMapper.selectByExample(null);
        List<Map<String, Object>> m = getZtreev(me,0);
        return m;
    }

    public List<Map<String, Object>> getZtreev(List<Menu> me, int pid) {
        List<Map<String, Object>> m = new ArrayList<>();
        for (int i = 0; i < me.size(); i++) {
            Map<String,Object> map = null;
            Menu n = me.get(i);
            if (n != null) {
                if (n.getParentid().equals(pid)) {
                    map = new HashMap<>();
                    map.put("name", n.getText());
                    map.put("id", n.getId());
                    map.put("children", getZtreev(me, n.getId()));
                }
            }
            if (map != null){
                m.add(map);
                List<Map<String,Object>> nodes =(List<Map<String,Object>>) map.get("children");
                if(nodes.size() <= 0){
                    map.remove("children");
                }

            }
        }
        return m;

    }

    @Override
    public List<Map<String, Object>> getInfo(Integer pid) {
            List<Menu> me = menuMapper.selectByPid(pid);
            List<Map<String, Object>> m = retr(me,0);
            return m;

    }
    public List<Map<String, Object>> retr(List<Menu> me, Integer pid) {
        List<Map<String, Object>> m = new ArrayList<>();
        for (int i = 0; i < me.size(); i++) {
            Map<String,Object> map = null;
            Menu n = me.get(i);
            if (n != null) {
                if (n.getParentid().equals(pid)) {
                    map = new HashMap<>();
                    map.put("text", n.getText());
                    map.put("id", n.getId());
                    map.put("url", n.getUrl());
                    map.put("nodes", retr(me, n.getId()));
                }
            }
            if (map != null){
                m.add(map);
                List<Map<String,Object>> nodes =(List<Map<String,Object>>) map.get("nodes");
                if(nodes.size() <= 0){
                    map.remove("nodes");
                }

            }
        }
        return m;
    }

    @Override
    public boolean addInfo(Menu menu) {
        if (menuMapper.insertSelective(menu)>0){
            return true;
        }
        return false;
    }

    @Override
    public Menu toUpdate(Integer id) {
        Menu menu = menuMapper.selectByPrimaryKey(id);
        return menu;
    }

    @Override
    public int deleteInfo(Integer id) {
        if (menuMapper.deleteByPrimaryKey(id) > 0){
            return 1;
        }
        return 2;
    }

    @Override
    public boolean updateInfo(Menu menu) {
            if (menuMapper.updateByPrimaryKeySelective(menu) > 0){
                return true;
            }
            return false;

    }

    @Override
    public List<String> selectByPid(Integer pid) {
        return menuMapper.selectPrmsById(pid);
    }

    @Override
    public List<Map<String, Object>> getZvChecked(Integer roleId) {
        List<Menu> me = menuMapper.selectByExample(null);
        List<Integer> ids = roleMenuMapper.selectByRid(roleId);
        return getZtreevChecked(me,0,ids);

    }
    public List<Map<String, Object>> getZtreevChecked(List<Menu> me, int pid,List<Integer> ids) {
        List<Map<String, Object>> m = new ArrayList<>();
        for (int i = 0; i < me.size(); i++) {
            Map<String, Object> map = null;
            Menu n = me.get(i);
            if (n != null) {
                if (n.getParentid().equals(pid)) {
                    map = new HashMap<>();
                    map.put("name", n.getText());
                    map.put("id", n.getId());
                    map.put("children", getZtreevChecked(me, n.getId(), ids));
                    if (ids.size() > 0 && ids != null && ids.contains(n.getId())) {
                        map.put("checked", true);
                    }
                    if (map != null) {
                        m.add(map);
                        List<Map<String, Object>> nodes = (List<Map<String, Object>>) map.get("children");
                        if (nodes.size() <= 0) {
                            map.remove("children");
                        }
                    }
                }
            }

        }
        return m;
    }
}
